1 /* 2 * The Apache Software License, Version 1.1 3 * 4 * Copyright (c) 2002 The Apache Software Foundation. All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. The end-user documentation included with the redistribution, 20 * if any, must include the following acknowledgment: 21 * "This product includes software developed by the 22 * Apache Software Foundation (http://www.apache.org/)." 23 * Alternately, this acknowledgment may appear in the software itself, 24 * if and wherever such third-party acknowledgments normally appear. 25 * 26 * 4. The names "Apache" and "Apache Software Foundation" must 27 * not be used to endorse or promote products derived from this 28 * software without prior written permission. For written 29 * permission, please contact apache@apache.org. 30 * 31 * 5. Products derived from this software may not be called "Apache", 32 * nor may "Apache" appear in their name, without prior written 33 * permission of the Apache Software Foundation. 34 * 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * ==================================================================== 48 * 49 * This software consists of voluntary contributions made by many 50 * individuals on behalf of the Apache Software Foundation. For more 51 * information on the Apache Software Foundation, please see 52 * <http://www.apache.org/>;. 53 */ 54 package net.sf.cglib.beans; 55 56 import net.sf.cglib.proxy.*; 57 import net.sf.cglib.core.Constants; 58 import net.sf.cglib.core.ReflectUtils; 59 import java.lang.reflect.Method; 60 import java.util.*; 61 import junit.framework.*; 62 63 public class TestBeanMap extends net.sf.cglib.CodeGenTestCase { 64 public static class TestBean { 65 private String foo; 66 private String bar = "x"; 67 private String baz; 68 private int quud; 69 private int quick = 42; 70 private int quip; 71 72 public String getFoo() { 73 return foo; 74 } 75 76 public void setFoo(String value) { 77 foo = value; 78 } 79 80 public String getBar() { 81 return bar; 82 } 83 84 public void setBaz(String value) { 85 baz = value; 86 } 87 88 public int getQuud() { 89 return quud; 90 } 91 92 public void setQuud(int value) { 93 quud = value; 94 } 95 96 public int getQuick() { 97 return quick; 98 } 99 100 public void setQuip(int value) { 101 quip = value; 102 } 103 } 104 105 public void testBeanMap() { 106 TestBean bean = new TestBean(); 107 BeanMap map = BeanMap.create(bean); 108 assertTrue(map.size() == 6); 109 assertTrue(map.get("foo") == null); 110 map.put("foo", "FOO"); 111 assertTrue("FOO".equals(map.get("foo"))); 112 assertTrue(bean.getFoo().equals("FOO")); 113 assertTrue("x".equals(map.get("bar"))); 114 assertTrue(((Integer)map.get("quick")).intValue() == 42); 115 map.put("quud", new Integer(13)); 116 assertTrue(bean.getQuud() == 13); 117 118 assertTrue(map.getPropertyType("foo").equals(String.class)); 119 assertTrue(map.getPropertyType("quud").equals(Integer.TYPE)); 120 assertTrue(map.getPropertyType("kdkkj") == null); 121 } 122 123 public void testNoUnderlyingBean() { 124 BeanMap.Generator gen = new BeanMap.Generator(); 125 gen.setBeanClass(TestBean.class); 126 BeanMap map = gen.create(); 127 128 TestBean bean = new TestBean(); 129 assertTrue(bean.getFoo() == null); 130 assertTrue(map.put(bean, "foo", "FOO") == null); 131 assertTrue(bean.getFoo().equals("FOO")); 132 assertTrue(map.get(bean, "foo").equals("FOO")); 133 } 134 135 public void testMixinMapIntoBean() { 136 Object bean = new TestBean(); 137 bean = mixinMapIntoBean(bean); 138 ((TestBean)bean).setFoo("hello"); 139 assertTrue(bean instanceof Map); 140 assertTrue(((Map)bean).get("foo").equals("hello")); 141 } 142 143 public void testRequire() { 144 BeanMap.Generator gen = new BeanMap.Generator(); 145 gen.setBeanClass(TestBean.class); 146 gen.setRequire(BeanMap.REQUIRE_GETTER); 147 BeanMap map = gen.create(); 148 assertTrue(map.containsKey("foo")); 149 assertTrue(map.containsKey("bar")); 150 assertTrue(!map.containsKey("baz")); 151 } 152 153 public static Object mixinMapIntoBean(final Object bean) { 154 Enhancer e = new Enhancer(); 155 e.setSuperclass(bean.getClass()); 156 e.setInterfaces(new Class[]{ Map.class }); 157 final Map map = BeanMap.create(bean); 158 e.setCallbackFilter(new CallbackFilter() { 159 public int accept(Method method) { 160 return method.getDeclaringClass().equals(Map.class) ? 1 : 0; 161 } 162 }); 163 e.setCallbacks(new Callback[]{ 164 new Dispatcher() { 165 public Object loadObject() { 166 return bean; 167 } 168 }, 169 new Dispatcher() { 170 public Object loadObject() { 171 return map; 172 } 173 } 174 }); 175 return e.create(); 176 } 177 178 // TODO: test different package 179 // TODO: test change bean instance 180 // TODO: test toString 181 182 public TestBeanMap(String testName) { 183 super(testName); 184 } 185 186 public static void main(String[] args) { 187 junit.textui.TestRunner.run(suite()); 188 } 189 190 public static Test suite() { 191 return new TestSuite(TestBeanMap.class); 192 } 193 }

This page was automatically generated by Maven